﻿using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sustainalytics.Claims;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.ESGRatings.BusinessLogic.ESGOnlineReport;
using Sustainalytics.ESGRatings.Service.Models;
using Sustainalytics.Utils;
using Sustainalytics.Auth.Claims;
using Sustainalytics.Utils.WebApi.ErrorCodes;
using System.Web.Http.Cors;

namespace Sustainalytics.ESGRatings.Service.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[ClaimsChecker("api/v1/company/esg")]
    //[AutoInvalidateCacheOutput]
    [LogControllerFilter(true, true, false)] 
    [RoutePrefix("api/v1/company")]
    public class EsgOverallPerformanceSummaryController : ApiController
    {
        private IOnlineReportGeneralBusinessLogicUnit<EsgOverallPerformanceSummary> _service;
        private IOnlineResearchProductsBusinessLogicUnit _businessLogicUnit;

        public EsgOverallPerformanceSummaryController(IOnlineReportGeneralBusinessLogicUnit<EsgOverallPerformanceSummary> service)
        {
            _service = service;
        }

        public EsgOverallPerformanceSummaryController()
        {
            Setup();
        }


       // [CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("{companyId}/esgOverallPerformanceSummary")]
        [UniverseChecker("universeIds", AccountFeature.EsgRatingsOnlineReport)]
        public HttpResponseMessage Get(Guid companyId, [FromUri]Guid esgProfileId, [FromUri]int[] universeIds = null)
        {
            try
            {
                if (esgProfileId == Guid.Empty) return Request.CreateResponse(HttpStatusCode.BadRequest,
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.InvalidEsgProfileId });

                var result = _businessLogicUnit.GetEsgOverallPerformanceSummary(companyId, esgProfileId).ToDto();


                return Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (ESGReasearchNotFoundException ex)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, new
                {
                    errCode = ex.GetType().ToString(),
                    message = ex.Message

                });

            }
            catch (ESGReasearchNotInUniversesException ex)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, new
                {
                    errCode = ex.GetType().ToString(),
                    message = ex.Message

                });
            }
            catch (Exception ex)
            {
                CommonEventSource.Log.Exception(ex);
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }

          
        }
        private void Setup()
        {
            var cnxSTring = ClientDWConnection.GetConnectionString();

            IRepositoryCollection<EsgOverallPerformanceSummary> repository = new RepositoryCollection<EsgOverallPerformanceSummary>(cnxSTring);

            _service = _service ?? new OnlineReportGeneralBusinessLogicUnit<EsgOverallPerformanceSummary>(repository);
            
            _businessLogicUnit = _businessLogicUnit ?? new OnlineResearchProductsBusinessLogicUnit(cnxSTring);
        }

    }
}
